package graphics;

import graphics.object.interfaces.GLKeyboardListener;
import graphics.object.interfaces.GLMouseListener;
import graphics.object.interfaces.GLRenderableObject;
import graphics.object.interfaces.GLTextListener;
import graphics.object.interfaces.GLTickListener;

import java.util.ArrayList;

public class GLObjectManager {

	public ArrayList<GLRenderableObject> renderableObjects = new ArrayList<GLRenderableObject>();
	public ArrayList<GLMouseListener> listenerMouseObjects = new ArrayList<GLMouseListener>();
	public ArrayList<GLKeyboardListener> listenerKeyboardObjects = new ArrayList<GLKeyboardListener>();
	public ArrayList<GLTextListener> listenerTextObjects = new ArrayList<GLTextListener>();
	public ArrayList<GLTickListener> listenerTickObjects = new ArrayList<GLTickListener>();

	public GLObjectManager(){}

	public void addObject(Object obj){
		if(obj instanceof GLRenderableObject){
			RenderPriority objPriority = ((GLRenderableObject)obj).getRenderPriority();
			switch(objPriority){
			case LOWEST:
				renderableObjects.add((GLRenderableObject)obj);
				break;
			case HIGHEST:
				renderableObjects.add(0, (GLRenderableObject)obj);
				break;
			default:
				if(renderableObjects.size() == 0){
					renderableObjects.add((GLRenderableObject)obj);
				}else{
					for(int i = 0; i < renderableObjects.size(); i++){
						if(objPriority.getValue() >= renderableObjects.get(i).getRenderPriority().getValue()){
							renderableObjects.add(i, (GLRenderableObject)obj);
							break;
						}else if(i == renderableObjects.size() - 1){
							renderableObjects.add((GLRenderableObject)obj);
						}
					}
				}
				break;
			}
		}if(obj instanceof GLMouseListener)
			listenerMouseObjects.add((GLMouseListener) obj);
		if(obj instanceof GLKeyboardListener)
			listenerKeyboardObjects.add((GLKeyboardListener) obj);
		if(obj instanceof GLTextListener)
            listenerTextObjects.add((GLTextListener) obj);
		if(obj instanceof GLTickListener)
		    listenerTickObjects.add((GLTickListener) obj);
	}

	public void removeObject(Object obj){
		if(obj instanceof GLRenderableObject)
			renderableObjects.remove(obj);
		if(obj instanceof GLMouseListener)
			listenerMouseObjects.remove(obj);
		if(obj instanceof GLKeyboardListener)
			listenerKeyboardObjects.remove(obj);
		if(obj instanceof GLTextListener)
            listenerTextObjects.remove(obj);
		if(obj instanceof GLTickListener)
		    listenerTickObjects.remove(obj);
	}
	
	public boolean hasObject(Object obj){
		if(renderableObjects.contains(obj)){
			return true;
		}else if(listenerMouseObjects.contains(obj)){
			return true;
		}else if(listenerKeyboardObjects.contains(obj)){
			return true;
		}else if(listenerTextObjects.contains(obj)){
            return true;
        }else if(listenerTickObjects.contains(obj)){
            return true;
        }else{
			return false;
		}
		
	}

	public enum RenderPriority{
		LOWEST(1),
		LOW(2),
		NORMAL(3),
		HIGH(4),
		HIGHEST(5);

		private int value = 0;

		RenderPriority(int i){
			value = i;
		}

		int getValue(){
			return value;
		}
	}
}